import {RigidBody, vec3} from '@react-three/rapier'
import {isHost} from 'playroomkit'
import {useEffect, useRef} from 'react'
import {MeshBasicMaterial} from 'three'
import {WEAPON_OFFSET} from './CharacterController'

const BULLET_SPEED = 20

const bulletMaterial = new MeshBasicMaterial({
  color: 'hotpink',
  toneMapped: false
})

bulletMaterial.color.multiplyScalar(42)

export const Bullet = ({player, angle, position, onHit}) => {
  const rigidBodyRef = useRef()

  useEffect(() => {
    const audio = new Audio('/audios/rifle.mp3')
    audio.play()
    const velocity = {
      x: Math.sin(angle) * BULLET_SPEED,
      y: 0,
      z: Math.cos(angle) * BULLET_SPEED
    }

    rigidBodyRef.current.setLinvel(velocity, true)
  }, [])

  return (
    <group position={[position.x, position.y, position.z]} rotation-y={angle}>
      <group
        position-x={WEAPON_OFFSET.x}
        position-y={WEAPON_OFFSET.y}
        position-z={WEAPON_OFFSET.z}
      >
        <RigidBody
          ref={rigidBodyRef}
          gravityScale={0}
          onIntersectionEnter={(e) => {
            // 如果子弹打中不是子弹
            if (isHost() && e.other.rigidBody.userData?.type !== 'bullet') {
              rigidBodyRef.current.setEnabled(false)
              onHit(vec3(rigidBodyRef.current.translation()))
            }
          }}
          sensor
          userData={{
            type: 'bullet',
            player,
            damage: 10
          }}
        >
          <mesh position-z={0.25} material={bulletMaterial} castShadow>
            <boxGeometry args={[0.05, 0.05, 0.5]}/>
          </mesh>
        </RigidBody>
      </group>
    </group>
  )
}
